package com.lanswon.common.util;

import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$";

	public static void main(String[] args) {
		System.out.println(md5("sara1234"));
	}

	/**
	 * 判断字符串是否为Long
	 */
	public static boolean isValidLong(String str) {
		try {
			@SuppressWarnings("unused")
			Long _v = Long.parseLong(str);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	/**
	 * @Title: isEmail
	 * @Description: TODO(是否是邮箱)
	 *
	 */
	public static boolean isEmail(String emailStr) {
		if (emailStr == null)
			return false;
		String[] splitEmail = emailStr.split("@");
		for (String str : splitEmail) {
			if (isEmpty(str)) {
				return false;
			}
		}
		String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		Pattern p;
		Matcher m;
		p = Pattern.compile(regEx1);
		m = p.matcher(emailStr);
		if (m.matches())
			return true;
		else
			return false;
	}

	/**
	 * @Title: getBirAgeSex
	 * @Description: TODO(获取身份证信息)
	 * @param certificateNo
	 * @return 返回的出生日期格式：1990-01-01 性别格式：F-女，M-男
	 *
	 */
	public static Map<String, Object> getBirAgeSex(String certificateNo) {
		String birthday = "";
		String age = "";
		String sexCode = "";

		int year = Calendar.getInstance().get(Calendar.YEAR);
		char[] number = certificateNo.toCharArray();
		boolean flag = true;
		if (number.length == 15) {
			for (int x = 0; x < number.length; x++) {
				if (!flag)
					return new HashMap<String, Object>();
				flag = Character.isDigit(number[x]);
			}
		} else if (number.length == 18) {
			for (int x = 0; x < number.length - 1; x++) {
				if (!flag)
					return new HashMap<String, Object>();
				flag = Character.isDigit(number[x]);
			}
		}
		if (flag && certificateNo.length() == 15) {
			birthday = "19" + certificateNo.substring(6, 8) + "-" + certificateNo.substring(8, 10) + "-"
					+ certificateNo.substring(10, 12);
			sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 3, certificateNo.length()))
					% 2 == 0 ? "女" : "男";
			age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
		} else if (flag && certificateNo.length() == 18) {
			birthday = certificateNo.substring(6, 10) + "-" + certificateNo.substring(10, 12) + "-"
					+ certificateNo.substring(12, 14);
			sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 4, certificateNo.length() - 1))
					% 2 == 0 ? "女" : "男";
			age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
		}
		Map<String, Object> map = new HashMap<>();
		map.put("birthday", birthday);
		map.put("age", age);
		map.put("sex", sexCode);
		return map;
	}

	/**
	 * @Title: containAlphanumeric
	 * @Description: TODO(包含字母数字)
	 * @param str
	 * @return
	 *
	 */
	public static boolean containAlphanumeric(String str) {
		boolean isDigit = false;// 定义一个boolean值，用来表示是否包含数字
		boolean isLetter = false;// 定义一个boolean值，用来表示是否包含字母
		for (int i = 0; i < str.length(); i++) { // 循环遍历字符串
			if (!isDigit && Character.isDigit(str.charAt(i))) { // 用char包装类中的判断数字的方法判断每一个字符
				isDigit = true;
			}
			if (!isLetter && Character.isLetter(str.charAt(i))) { // 用char包装类中的判断字母的方法判断每一个字符
				isLetter = true;
			}
		}
		if (isLetter && isDigit) {
			return true;
		}
		return false;
	}

	/**
	 * @Title: isDate
	 * @Description: TODO(是否为常用日期类型)
	 * @param strDate
	 *            例：20181120 2018-11-20 2018/11/20
	 * @return
	 *
	 */
	public static boolean isDate(String strDate) {
		Pattern pattern = Pattern.compile(
				"^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
		Matcher m = pattern.matcher(strDate);
		if (m.matches()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @Title: isIdNum
	 * @Description: TODO(是否为身份证号)
	 * @param idNum
	 * @return
	 *
	 */
	public static boolean isIdNum(String idNum) {
		// 对身份证号进行长度等简单判断
		if (idNum == null || idNum.length() != 18 || !idNum.matches("\\d{17}[0-9X]")) {
			return false;
		}
		// 1-17位相乘因子数组
		int[] factor = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
		// 18位随机码数组
		char[] random = "10X98765432".toCharArray();
		// 计算1-17位与相应因子乘积之和
		int total = 0;
		for (int i = 0; i < 17; i++) {
			total += Character.getNumericValue(idNum.charAt(i)) * factor[i];
		}
		// 判断随机码是否相等
		return random[total % 11] == idNum.charAt(17);
	}

	public static String makePhoneCode(String mobile) {
		Random random = new Random();
		String registerCode = "";
		for (int i = 0; i < 6; i++) {
			registerCode += random.nextInt(10);
		}
		return registerCode;
	}

	/**
	 * @Title: isMobile
	 * @Description: TODO(是否为手机号)
	 * @param mobile
	 * @return
	 */
	public static boolean isMobile(String mobile) {
		return Pattern.matches(REGEX_MOBILE, mobile);
	}

	/**
	 * @Title: isEmpty
	 * @Description: TODO(是否为空字符串)
	 * @param str
	 * @return
	 *
	 */
	public static boolean isEmpty(String str) {
		if ("".equals(str) || str == null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @Title: strToStrArray
	 * @Description: TODO(字符串转换为字符串数组)
	 * @param str
	 * @param splitRegex
	 * @return
	 *
	 */
	public static String[] strToStrArray(String str, String splitRegex) {
		if (isEmpty(str)) {
			return null;
		}
		return str.split(splitRegex);
	}

	/**
	 * @Title: strToDate
	 * @Description: TODO(固定日期格式字符串转换为日期)
	 * @param strDate
	 * @param dateFormatType
	 * @return
	 *
	 */
	public static Date strToDate(String strDate, String dateFormatType) {
		if (!isEmpty(strDate)) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat(dateFormatType);
				return sdf.parse(strDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return new Date();
		} else {
			return null;
		}
	}

	/**
	 * @Title: putIntoMap
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param map
	 * @param name
	 * @param object
	 *
	 */
	public static void putIntoMap(Map<String, Object> map, String name, Object object) {
		if (object != null) {
			map.put(name, object);
		} else {
			map.put(name, "");
		}
	}

	public static String strToMd5(String str) {
		String temp = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] mdb = md.digest(str.getBytes());
			temp = convertByteToHexString(mdb);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return temp;
	}

	public static String convertByteToHexString(byte[] bytes) {
		String results = "";
		for (byte b : bytes) {
			int temp = b & 0xff;
			String tempHex = Integer.toHexString(temp);
			if (tempHex.length() < 2) {
				results += "0" + tempHex;
			} else {
				results += tempHex;
			}
		}
		return results;
	}

	/**
	 * @param credential
	 * @return
	 */
	public static String md5(String credential) {
		return strToMd5(convertByteToHexString(strToMd5(convertByteToHexString(credential.getBytes())).getBytes()));
	}
}
